/**
 * @author yxw
 * date : 2013年9月30日 下午3:50:36 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.tzinfo.data.TztData;
import com.tzinfo.data.UserInfo;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tztbase.TztBaseLoginActivity;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class MyPageActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {

	private SimpleAdapter simpleAdapter;
	private Button updateBtn;
	private GridView gridView;
	private List<Map<String, Object>> data;
	private UserInfo userInfo;
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_page);
		init();
	}

	private void init() {
		updateBtn = (Button) this.findViewById(R.id.bt_update);
		updateBtn.setOnClickListener(this);
		gridView = (GridView) this.findViewById(R.id.grid_my_page);
		getDate();
		simpleAdapter = new SimpleAdapter(this, data,
				R.layout.item_my_page_grid, new String[] { "img", "text" },
				new int[] { R.id.grid_image, R.id.grid_text });
		gridView.setAdapter(simpleAdapter);
		gridView.setOnItemClickListener(this);
	}


	private void getDate() {
		data = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		for (int i = 0; i < 8; i++) {
			map = new HashMap<String, Object>();
			map.put("img", R.drawable.my_50icon01 + i);
			map.put("text", getString(R.string.mypage_grid01 + i));
			data.add(map);
		}
	}

	private void umengUpdate() {
		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
		dialog.show();
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
			@Override
			public void onUpdateReturned(int updateStatus,
					UpdateResponse updateInfo) {
				dialog.dismiss();
				switch (updateStatus) {
				case 0: // has update
					UmengUpdateAgent.showUpdateDialog(MyPageActivity.this,
							updateInfo);
					break;
				case 1: // has no update
					Toast.makeText(MyPageActivity.this, "没有更新",
							Toast.LENGTH_SHORT).show();
					break;
				case 2: // none wifi
					Toast.makeText(MyPageActivity.this, "没有wifi连接， 只在wifi下更新",
							Toast.LENGTH_SHORT).show();
					break;
				case 3: // time out
					Toast.makeText(MyPageActivity.this, "网络超时",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_update:
			UmengUpdateAgent.forceUpdate(this);
			umengUpdate();
			break;
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// arg2从0开始
		switch (arg2) {
		case 0:
			Intent collectionIntent = new Intent(this,
					MyCollectionActivity.class);
			startActivity(collectionIntent);
			break;
		case 1:
			Intent historyIntent = new Intent(this, MyHistoryActivity.class);
			startActivity(historyIntent);
			break;
		case 2:
			Intent callIntent = new Intent(this, MyCallActivity.class);
			startActivity(callIntent);
			break;
		case 3:
			Intent tuanIntent = new Intent(this, MyTuanActivity.class);
			startActivity(tuanIntent);
			break;
		case 4:
			Intent aboutIntent = new Intent(this, AboutUsActivity.class);
			startActivity(aboutIntent);
			break;
		case 5:
			Intent falsityIntent = new Intent(this,
					FalseTztInformationActivity.class);
			startActivity(falsityIntent);
			break;
		case 6:
			Intent intent = new Intent(this, SelectCityActivity.class);
			startActivity(intent);
			break;
		case 7:
			if (TztData.userInfo != null) {
				Intent intentFabu = new Intent(this, MyFabuActivity.class);
				startActivity(intentFabu);
			} else {
				new AlertDialog.Builder(this)
						.setCancelable(false)
						.setMessage("您还没登录，请先登录！")
						.setPositiveButton(getResources().getString

						(R.string.cancel),
								new DialogInterface.OnClickListener()

								{
									public void onClick(DialogInterface dialog,
											int which) {
									}
								})
						.setNegativeButton("前往登录",
								new DialogInterface.OnClickListener()

								{
									public void onClick(

									DialogInterface dialogInterface, int

									whichButton) {
										Intent intent = new Intent(
												MyPageActivity.this,
												TztBaseLoginActivity.class);
										startActivityForResult(intent, 1);
									}
								}).show();
			}

			break;
		default:
			break;
		}
	}

}
